package com.paso.login.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.paso.login.pojo.Token;
import com.paso.login.pojo.User;
import com.paso.login.pojo.UserInfo;
import com.paso.login.service.IUserService;
import com.paso.login.utils.MD5Utils;
import com.paso.login.vo.VoUser;
import com.paso.mycommons.utils.JwtUtil;
import com.paso.mycommons.vo.RespBean;
import com.paso.mycommons.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangx
 * @since 2021-02-12
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    IUserService userService;

    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }




    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public RespBean login(VoUser user) {

        RespBean respBean = new RespBean();

        log.info("前端传入用户账户：" + user.getUsername());
        log.info("前端出入用户密码：" + user.getPassword()); //198737fcf5d3815e19ec6414932a3f09

        try {
            // 通过前端获取的VoUser查询出数据库中的User
            User user1 = new User();
            user1.setUsername(user.getUsername());
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user1);
            User userDB = userService.getOne(userQueryWrapper);
            log.info("数据库用户ID：" + userDB.getId());
            log.info("数据库用户账户：" + userDB.getUsername());
            log.info("数据库用户密码：" + userDB.getPassword()); //dea1592bdb66f24fc938fa734beb99a1

            // 判断传入的用户和密码是否和数据库中的相同
            if (userDB.getUsername().equals(user.getUsername()) && userDB.getPassword().equals(MD5Utils.formPassToDBPass(user.getPassword(),"1a2b3c4d"))) {

                // 生成token
                String token = JwtUtil.sign(user.getUsername());
                respBean.setCode(20000);
                respBean.setMessage("success");
                respBean.setData(new Token(token));
            } else {
                respBean.setCode(500);
                respBean.setMessage("用户名或密码错误");
                respBean.setData("fail");
            }


        } catch (Exception e) {
            respBean.setCode(RespBeanEnum.ERROR.getCode());
            respBean.setMessage(respBean.getMessage());
        }

        return respBean;
//        return new RespBean(20000, RespBeanEnum.SUCCESS.getMessage(), new Token("admin-token"));
    }


    @GetMapping("/info")
    public RespBean loginInfo(@RequestParam("token") String token) {

        log.info("info里面的token:" + token);
        UserInfo userInfo = new UserInfo();
        ArrayList<String> list = new ArrayList<>();
        list.add("admin");
        userInfo.setIntroduction("I am a super administrator");
        userInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        userInfo.setName("Super Admin");
        userInfo.setRoles(list);

        return new RespBean(20000, RespBeanEnum.SUCCESS.getMessage(), userInfo);
    }


    @PostMapping("/logout")
    public RespBean logOut(@RequestHeader("X-Token") String token) {


        return new RespBean();
    }
}
